﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkLib.Facades;
using FrameworkLib.Configurations.FormsConfiguration.DAL;
using FrameworkLib.Configurations.FormsConfiguration.BL;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace FrameworkLib.Configurations.FormsConfiguration.Facade
{
    public class FormsConfiguration2Facade : FacadeBase<FormsConfigurationEntities>
    {
        FormsConfigurationEntities context = null;
        private string m_errorOccurred = string.Empty;

        public string ErrorOccurred
        {
            get { return m_errorOccurred; }
            set { m_errorOccurred = value; }
        }

        public FormsConfiguration2Facade()
        {
            context = this.GetContext();
        }

        public FormsConfiguration2Facade(string connectionString)
        {
            context = this.GetContext(connectionString);
        }

        public static FormConfiguration CreateFormConfiguration(string formName)
        {
            FormConfiguration retValue = new FormConfiguration();
            retValue.FormName = formName;

            DAL.FormsList2 fconf = new DAL.FormsList2();
            fconf.FormName = formName;

            FormsConfigurationEntities context = new FormsConfigurationEntities();
            context.FormsList2.AddObject(fconf);
            context.SaveChanges();

            retValue.ID = fconf.IDForm;

            return retValue;
        }

        public bool Delete(short id)
        {
            bool retValue = false;

            var obj = from x in context.FormsList2 where x.IDForm == id select x;

            if (obj.Any())
            {
                context.FormsList2.DeleteObject(obj.FirstOrDefault());

                context.SaveChanges();

                retValue = true;
            }

            //
            return retValue;
        }

        public List<FormConfiguration> GetForms()
        {
            List<FormConfiguration> retValue = null;

            var objs = from x in context.FormsList2
                       select new FormConfiguration
                       {
                           ID = x.IDForm,
                           FormName = x.FormName.Trim(),
                           RoleName = x.RoleName.Trim(),
                           UserName = x.UserName.Trim()
                       };

            if (objs.Any())
            {
                retValue = objs.ToList();
            }

            return retValue;
        }

        public FormConfiguration GetForm(string formName)
        {
            FormConfiguration retValue = null;

            var objs = from x in context.FormsList2 where x.FormName == formName select new FormConfiguration { ID = x.IDForm, FormName = x.FormName };

            if (objs.Any())
            {
                retValue = objs.FirstOrDefault();
            }

            return retValue;
        }

        public bool SaveForm(List<FormConfiguration> forms)
        {
            bool retValue = false;

            this.Transact(() =>
            {
                foreach (FormConfiguration ff in forms)
                {
                    DAL.FormsList2 fconf = new DAL.FormsList2();
                    fconf.FormName = ff.FormName;

                    context.FormsList2.AddObject(fconf);
                }

                context.SaveChanges();

                retValue = true;
            });

            return retValue;
        }

        public bool SaveColumnsCfg(SavingConfigurationBag configuration, string state)
        {
            bool retValue = false;

            try
            {
                DAL.FormsList2 fconf = null;

                IQueryable<FormsList2> query = context.FormsList2.Where(x => x.FormName == configuration.FormName);

                if (!string.IsNullOrWhiteSpace(configuration.RoleName))
                {
                    query = query.Where(x => x.RoleName == configuration.RoleName);
                }

                if (!string.IsNullOrWhiteSpace(configuration.UserName))
                {
                    query = query.Where(x => x.UserName == configuration.UserName);
                }

                fconf = query.FirstOrDefault();
                if (fconf == null)
                {
                    fconf = new DAL.FormsList2();
                    context.FormsList2.AddObject(fconf);
                }

                fconf.FormName = configuration.FormName;
                fconf.RoleName = (string.IsNullOrWhiteSpace(configuration.RoleName) ? null : configuration.RoleName);
                fconf.UserName = (string.IsNullOrWhiteSpace(configuration.UserName) ? null : configuration.UserName);
                fconf.SerializedXmlGridData = state;

                context.SaveChanges();

                retValue = true;
            }
            catch (Exception ex)
            {
                ErrorOccurred = ex.Message;
            }

            return retValue;
        }

        public string GetColumnsCfg(string formName)
        {
            string retValue = null;

            var obj = from x in context.FormsList2
                      where x.FormName == formName
                      select x;

            if (obj.Any())
            {
                DAL.FormsList2 fconf = obj.FirstOrDefault();

                retValue = fconf.SerializedXmlGridData;
            }

            return retValue;
        }

        public string GetColumnsCfg(SavingConfigurationBag configuration)
        {
            string retValue = null;

            if (configuration != null)
            {
                IQueryable<FormsList2> query = context.FormsList2.Where(x => x.FormName == configuration.FormName);

                if (!string.IsNullOrWhiteSpace(configuration.RoleName))
                {
                    query = query.Where(x => x.RoleName == configuration.RoleName);
                }

                if (!string.IsNullOrWhiteSpace(configuration.UserName))
                {
                    query = query.Where(x => x.UserName == configuration.UserName);
                }

                DAL.FormsList2 fconf = query.FirstOrDefault();
                if (fconf != null)
                {
                    retValue = fconf.SerializedXmlGridData;
                }
            }

            return retValue;
        }


    }
}
